package net.danielpaz.pucc.poo.trabalho.model;

import java.sql.*;

public class VendaDAO {
	// public Album album;
	public BD bd;
	private PreparedStatement statement;
	private ResultSet resultSet;
	private String men, sql;
	public static final byte INCLUSAO = 1;
	public static final byte ALTERACAO = 2;
	public static final byte EXCLUSAO = 3;

	public VendaDAO() {
		bd = new BD();
		// album = new Album();
	}

	public boolean localizar(int operacao, Venda venda ) {
		sql = "select * from gerente where codigo = ?";
		try {
			statement = bd.connection.prepareStatement(sql);
			// statement.setString(1,
			// Integer.toString(gerente.getId_gerente()));
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				// gerente.setId_gerente(resultSet.getString(1));
				// gerente.setNome(resultSet.getString(2));
			}
			return true;
		} catch (SQLException erro) {
			erro.printStackTrace();
			return false;
		}
	}

	public String atualizar(int operacao, Venda venda) {
		men = "Operacao realizada com sucesso!";
		try {
			if (operacao == INCLUSAO) {
				sql = "insert into venda values (?,?,?,?)";
				statement = bd.connection.prepareStatement(sql);
				statement.setString(1, Integer.toString(venda.getId()));
				statement.setString(2, Integer.toString(venda.getIdVendedor()));
				statement.setString(3, Integer.toString(venda.getIdAlbum()));
				statement.setString(4, venda.getData());

			} else if (operacao == ALTERACAO) {
				sql = "update venda set idvendedor = ?, idalbum = ?, data = ? where id = ?";
				statement = bd.connection.prepareStatement(sql);
				statement.setString(1, Integer.toString(venda.getIdVendedor()));
				statement.setString(2, Integer.toString(venda.getIdAlbum()));
				statement.setString(3, venda.getData());
				statement.setString(4, Integer.toString(venda.getId()));

			} else if (operacao == EXCLUSAO) {
				sql = "delete from venda where id = ?";
				statement = bd.connection.prepareStatement(sql);
				statement.setString(1, Integer.toString(venda.getId()));
			}
			if (statement.executeUpdate() == 0)
				men = "Falha na operacao!";
		} catch (SQLException erro) {
			men = "Falha na operacao " + erro.toString();
		}
		return men;
	}

}
